package com.sina.shop.manage.controller.menu;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.menu.Menu;
import com.sina.shop.manage.service.menu.MenuManageService;
import com.sina.shop.web.BaseController;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-03-14:18
 */
@Controller
@RequestMapping("menuManage")
public class MenuManageController extends BaseController {

    @Autowired
    @Qualifier("menuManageService")
    private MenuManageService menuManageService;

    @RequestMapping(value = "getMenuList", method = RequestMethod.GET)
    public ModelAndView getMenuList(@Valid Menu menu) {
        LOG.info("menu:{}", menu);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];
        List<Map<String, Object>> permissionList = menuManageService.getMenuList(menu,count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "getMenuIdAndNameList", method = RequestMethod.GET)
    public ModelAndView getMenuIdAndNameList(Boolean hasAll) {

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);

        List<Map<String, Object>> permissionList = menuManageService.getMenuIdAndNameList(hasAll);

        Map<String, Object> data = new HashMap<>(4);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "getMenu", method = RequestMethod.GET)
    public ModelAndView getMenu(String name) {
        LOG.info("name:{}", name);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        Menu menu = menuManageService.getMenuByName(name);
        result.put("data", menu);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "deleteMenu", method = RequestMethod.POST)
    public ModelAndView deleteMenu(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            menuManageService.deleteMenu(id);
            result.put(CODE, OK);
            result.put(MSG, "删除菜单成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "updateMenu", method = RequestMethod.POST)
    public ModelAndView updateMenu(@Valid Menu menu) {

        LOG.info("menu:{}", menu);

        //设置修改时间
        menu.setUpdateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            menuManageService.updateMenu(menu);
            result.put(CODE, OK);
            result.put(MSG, "更新菜单成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "saveMenu", method = RequestMethod.POST)
    public ModelAndView saveMenu(@Valid Menu menu, String pids) {

        LOG.info("menu:{}", menu);

        /*创建时间、修改时间、创建人*/
        menu.setCreateTime(LocalDateTime.now());
        menu.setUpdateTime(LocalDateTime.now());
        String createName = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
        menu.setCreateName(createName);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            menuManageService.saveMenu(menu);
            result.put(CODE, OK);
            result.put(MSG, "新增菜单成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "getMenuListToHtml", method = RequestMethod.GET)
    private ModelAndView getMenuListToHtml() {

        ModelAndView mv = new ModelAndView("menu/menuList");

        return mv;
    }
}
